package com.lagou.service.impl;

import com.lagou.dao.RoleMapper;
import com.lagou.domain.*;
import com.lagou.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;


    @Override
    public List<Role> findAllRole(Role role) {

        List<Role> allRole = roleMapper.findAllRole(role);

        return allRole;
    }

    @Override
    public List<Integer> findMenuByRoleId(Integer roleId) {

        List<Integer> menuByRoleId = roleMapper.findMenuByRoleId(roleId);

        return menuByRoleId;
    }

    @Override
    public void saveRole(Role role) {


        role.setCreatedBy("1841567847");
        role.setUpdatedBy("1562345685");

        Date date = new Date();
        role.setCreatedTime(date);
        role.setUpdatedTime(date);


        roleMapper.saveRole(role);

    }

    @Override
    public Role findRoleById(Integer id) {

        Role roleById = roleMapper.findRoleById(id);

        return roleById;
    }

    @Override
    public void updateRole(Role role) {

        role.setCreatedBy("1841567847");
        role.setUpdatedBy("1562345685");

        Date date = new Date();
        role.setCreatedTime(date);
        role.setUpdatedTime(date);
        roleMapper.updateRole(role);
    }

    @Override
    public void deleteRoleContextMenu(Integer rid) {

    }


    @Override
    public void roleContextMenu(RoleMenuVo roleMenuVo) {

        //1.清空中间表的关联关系
        roleMapper.deleteRoleContextMenu(roleMenuVo.getRoleId());

        //2.为角色分配菜单

        List<Integer> menuIdList = roleMenuVo.getMenuIdList();

        for (Integer mid : menuIdList) {

            Role_menu_relation role_menu_relation = new Role_menu_relation();

            role_menu_relation.setMenuId(mid);
            role_menu_relation.setRoleId(roleMenuVo.getRoleId());

            //封装数据
            Date date = new Date();
            role_menu_relation.setCreatedTime(date);
            role_menu_relation.setUpdatedTime(date);

            role_menu_relation.setCreatedBy("system");
            role_menu_relation.setUpdatedby("system");

            roleMapper.roleContextMenu(role_menu_relation);

        }


    }

    @Override
    public void deleteRole(Integer roleId) {

        //调用根据roleId清空中间表方法
        roleMapper.deleteRoleContextMenu(roleId);

        roleMapper.deleteRole(roleId);
    }




    /**
     * 1.查询当前角色拥有的资源
     * @return
     */
    @Override
    public List<ResourceCategory> findResourceListByRoleId(Integer roleId) {

        //1.获取角色拥有的资源分类数据
        List<ResourceCategory> categoryList = roleMapper.findResourceCategoryByRoleId(roleId);

        //2.获取角色拥有的资源数据
        List<Resource> resourceList = roleMapper.findResourceListByRoleId(roleId);

        //3.将资源数据封装到对应分类下
        for (ResourceCategory category : categoryList) {
            for (Resource resource : resourceList) {

                //判断
                if (category.getId() == resource.getCategoryId()){
                    //将资源保存到集合中
                   category.getResourceList().add(resource);
                }
            }
        }

        return categoryList;
    }


    /**
     * 分配资源
     * @param roleResourceVo
     */
    @Override
    public void roleContextResource(RoleResourceVo roleResourceVo) {
        //1.根据角色id清空中间表关联关系
        roleMapper.deleteRoleContextResource(roleResourceVo.getRoleId());

        //2.在重新建立关联关系
        List<Integer> resourceIdList = roleResourceVo.getResourceIdList();
        for (Integer resourceId : resourceIdList) {

            //封装数据
            Role_Resource_relation role_resource_relation = new Role_Resource_relation();
            role_resource_relation.setRoleId(roleResourceVo.getRoleId());
            role_resource_relation.setResourceId(resourceId);

            Date date = new Date();
            role_resource_relation.setCreatedTime(date);
            role_resource_relation.setUpdatedTime(date);

            role_resource_relation.setCreatedBy("system");
            role_resource_relation.setUpdatedby("system");

            roleMapper.roleContextResource(role_resource_relation);
        }
    }
}
